package com.wuzihao.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuzihao.reggie.dto.DishDto;
import com.wuzihao.reggie.entity.Category;
import com.wuzihao.reggie.entity.Dish;
import com.wuzihao.reggie.entity.DishFlavor;
import com.wuzihao.reggie.mapper.DishMapper;
import com.wuzihao.reggie.service.CategoryService;
import com.wuzihao.reggie.service.DishFlavorService;
import com.wuzihao.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService{

    @Autowired
    DishService dishService;

    @Autowired
    DishFlavorService dishFlavorService;

    @Autowired
    CategoryService categoryService;

    //TODO 保护数据一致性
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        dishService.save(dishDto);
        //Dish中没有dish_id这个字段
        Long dish_id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map(item -> {
            item.setDishId(dish_id);
            return item;
        }).collect(Collectors.toList());
        //批量保存口味数据
        dishFlavorService.saveBatch(collect);
    }

    //TODO
    //调用dishService.page执行分页查询,返回封装后dishDtoPage给页面成功展示categoryName属性数据
    @Override
    public Page<DishDto> pageInfo(int page, int pageSize, String name) {
        Page<Dish> dishPage = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        //构造器查询
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(name != null,"name",name);
        queryWrapper.orderByDesc("update_time");
        //执行分页查询
        dishService.page(dishPage,queryWrapper);

        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");

        //重新封装records,给dishDto封装categoryName属性
        List<Dish> records = dishPage.getRecords();
        List<DishDto> collect = records.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            if (category != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(collect);

        return dishDtoPage;
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = dishService.getById(id);
        DishDto dishDto = new DishDto();
        //属性拷贝
        BeanUtils.copyProperties(dish,dishDto);
        List<DishFlavor> list = dishFlavorService.list(new QueryWrapper<DishFlavor>().eq("dish_id", id));
        //设置flavor属性
        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {
        // TODO update和updateById的区别
        //更新dish表
        dishService.updateById(dishDto);

        Long dishId = dishDto.getId();
        //删除原先的口味
        dishFlavorService.remove(new QueryWrapper<DishFlavor>().eq("dish_id",dishId));
        //插入修改过后的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map(item -> {
            //dishDto继承Dish没有dish_id字段，dishFlavor中有dish_id字段，重新设置dish_id字段
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //批量保存口味
        dishFlavorService.saveBatch(collect);
    }

    @Override
    public List<DishDto> listDish(Dish dish) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,"category_id",dish.getCategoryId());
        //查询状态 0停售 1起售
        queryWrapper.eq("status",1);
        queryWrapper.orderByAsc("sort").orderByDesc("update_time");

        List<Dish> list = dishService.list(queryWrapper);
        //重新封装dishDto类
        List<DishDto> collect = list.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            //设置categoryName
            Category category = categoryService.getById(item.getCategoryId());
            if (category != null) {
                dishDto.setCategoryName(category.getName());
            }
            //设置flavor属性
            List<DishFlavor> flavorList = dishFlavorService.list(new QueryWrapper<DishFlavor>().eq("dish_id", item.getId()));
            dishDto.setFlavors(flavorList);

            return dishDto;
        }).collect(Collectors.toList());

        return collect;
    }

}
